package com.woniuxy.myshop.contrloller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.myshop.entity.Product;
import com.woniuxy.myshop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    ProductService productService ;

    @RequestMapping("/list")
    public String list(@RequestParam(name = "now",defaultValue = "1") Integer now,
                                  @RequestParam(name = "size",defaultValue = "3") Integer size,
                                  String pname, Map map){
        PageInfo<Product> pageInfo = productService.queryByPage(now,size,pname);
        map.put("pageInfo",pageInfo);
        map.put("pname",pname);
        return "product" ;
    }


    @RequestMapping("/listJson")
    @ResponseBody
    public PageInfo<Product> listJson(@RequestParam(name = "now",defaultValue = "1") Integer now,
                       @RequestParam(name = "size",defaultValue = "3") Integer size,
                       String pname, Map map){
        PageInfo<Product> pageInfo = productService.queryByPage(now,size,pname);

        return pageInfo ;
    }


    @RequestMapping("/getById")
    @ResponseBody
    public Product getById(int id){
        Product product =  productService.queryProductByKey(id);
        return product ;
    }

    @RequestMapping("/updateById")
    @ResponseBody
    public Product updateById(int id){
        Product product =  productService.queryProductByKey(id);
        product.setPrice(3.01);

        //修改
        Product p1 = productService.modify(product);
        return p1 ;
    }

    @RequestMapping("/delById")
    @ResponseBody
    public int delById(int id){

        return productService.removeByKey(id) ;
    }








}
